import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import LoginView from "../views/LoginView.vue";
import IndexView from "../views/IndexView.vue";
// import HealthAssessment from "";

Vue.use(VueRouter);

let router= new VueRouter({
  routes:[
      {path:"/login",component: LoginView,meta: { isokpath:true }},
      {path:"/home",component: HomeView,
      children:[{
        path:"index",component:IndexView
      }]},

      //健康评估
      {
        path: '/home',
        component: HomeView,
        meta: {
          title: "健康评估",
        },
        children: [
          // {
          //   path: 'physiqueEvaluation',
          //   component: () => import('@/views/physiqueEvaluation.vue'),
          //   name: 'healthAssessment',
          //   meta: {
          //     title: "体质测评",
          //   }
          // },
          {
            path: 'mentalEvaluation',
            component: () => import('@/views/mentalEvaluation.vue'),
            name: 'healthAssessment',
            meta: {
              title: "心理测评",
            }
          }
        ]
      },
      {path:"/",redirect: "/login",meta: { isokpath:true }}
  ]
})
router.beforeEach((to, from, next) => {
  //用户如果已经登陆，放行
  //用户需要登录，应该放行
  // 路由中有 isokpath属性，表示因该放行的路由(url)
 /* console.log(to)*/

  if(to.matched.some(val=> val.meta.isokpath==true )){
         next()
  }else{
        if(sessionStorage.getItem("name")){
             next();
        }else{
        next({path:"/login"});
     }
  }
})
export default router;
